package com.floreantpos.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumnModel;

import com.floreantpos.bo.ui.explorer.ListTableModel;
import com.floreantpos.main.Application;
import com.floreantpos.model.Ticket;
import java.text.SimpleDateFormat;

public class TicketListView extends JPanel {

  private JTable table;
  private TicketListTableModel tableModel;

  public TicketListView() {
    table = new TicketListTable();
    table.setModel(tableModel = new TicketListTableModel());
    table.setRowHeight(40);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    table.setDefaultRenderer(Object.class, new PosTableRenderer());
    table.setGridColor(Color.LIGHT_GRAY);

    TableColumnModel columnModel = table.getColumnModel();
    columnModel.getColumn(0).setPreferredWidth(40);
    columnModel.getColumn(1).setPreferredWidth(110);
    columnModel.getColumn(2).setPreferredWidth(110);
    columnModel.getColumn(3).setPreferredWidth(90);

    JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
    scrollBar.setPreferredSize(new Dimension(30, 60));

    setLayout(new BorderLayout());

    add(scrollPane);
  }

  public void setTickets(List<Ticket> tickets) {
    tableModel.setRows(tickets);
  }

  public void addTicket(Ticket ticket) {
    tableModel.addItem(ticket);
  }

  public Ticket getSelectedTicket() {
    int selectedRow = table.getSelectedRow();
    if (selectedRow < 0) {
      return null;
    }

    return (Ticket) tableModel.getRowData(selectedRow);
  }

  public List<Ticket> getSelectedTableTickets() {
    ArrayList<Ticket> tickets = new ArrayList<Ticket>();

    int selectedRow = table.getSelectedRow();
    if (selectedRow < 0) {
      return tickets;
    }

    Ticket selectedTicket = (Ticket) tableModel.getRowData(selectedRow);
    int selectedTicketTableNumber = selectedTicket.getTableNumber();

    if (selectedTicketTableNumber == -1) {
      Ticket ticket = (Ticket) tableModel.getRowData(selectedRow);
      tickets.add(ticket);
      return tickets;
    }

    if (Application.isSaveGuestName()) {
      String selectedTicketGuestName = selectedTicket.getGuest();
      for (int i = 0; i < table.getRowCount(); i++) {
        Ticket ticket = (Ticket) tableModel.getRowData(i);
        String ticketGuestName = ticket.getGuest();
        if (selectedTicketTableNumber > 0 && ticket.getTableNumber() > 0 && selectedTicketGuestName.equals(ticketGuestName)) {
          tickets.add(ticket);
        }
      }
    } else {
      for (int i = 0; i < table.getRowCount(); i++) {
        Ticket ticket = (Ticket) tableModel.getRowData(i);
        int ticketTableNumber = ticket.getTableNumber();
        if (selectedTicketTableNumber > 0 && selectedTicketTableNumber == ticketTableNumber) {
          tickets.add(ticket);
        }
      }
    }
    return tickets;
  }

  public List<Ticket> getSelectedTickets() {
    int[] selectedRows = table.getSelectedRows();

    ArrayList<Ticket> tickets = new ArrayList<Ticket>(selectedRows.length);

    for (int i = 0; i < selectedRows.length; i++) {
      Ticket ticket = (Ticket) tableModel.getRowData(selectedRows[i]);
      tickets.add(ticket);
    }
    return tickets;
  }

  private class TicketListTable extends JTable {

    @Override
    public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
      ListSelectionModel selectionModel = getSelectionModel();
      boolean selected = selectionModel.isSelectedIndex(rowIndex);
      if (selected) {
        selectionModel.removeSelectionInterval(rowIndex, rowIndex);
      } else {
        selectionModel.addSelectionInterval(rowIndex, rowIndex);
      }
    }
  }

  private class TicketListTableModel extends ListTableModel {

    public TicketListTableModel() {
      super(new String[]{com.floreantpos.POSConstants.ID, com.floreantpos.POSConstants.TABLE, com.floreantpos.POSConstants.SERVER, com.floreantpos.POSConstants.CREATED, com.floreantpos.POSConstants.TOTAL, com.floreantpos.POSConstants.DUE});
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
      Ticket ticket = (Ticket) rows.get(rowIndex);

      switch (columnIndex) {
        case 0:
          return String.valueOf(Integer.valueOf(ticket.getId()));

        case 1:
          return ticket.getGuest();

        case 2:
          return String.valueOf(ticket.getOwner());

        case 3:
          return new SimpleDateFormat("dd/MM/yyyy HH:mm").format(ticket.getCreateDate());

        case 4:
          return ticket.getTotalWithGratuity();

        case 5:
          return ticket.getDueAmountWithGratuity();

      }

      return null;
    }

  }
}
